package com.atguigu.bigdata.scala.chapter08

object Scala06_Match {

    def main(args: Array[String]): Unit = {
        // TODO - 模式匹配 - 对象匹配

        // 模式匹配中：如果匹配对象，那么需要特殊的操作
        // 匹配对象的目的：为了匹配对象的属性是否完全相同
        // unapply方法 => 对象 => 属性 => name = zhangsan, age = 30

        // apply方法 => 属性 => 对象
        val user = getUser()

        user match {
            case User("zhangsan", 30) => println("用户是张三")
            case User("lisi", 30) => println("用户是李四")
            case _ => println("谁也不是")
        }

    }
    class User {
        var name:String = _
        var age : Int = _
    }
    object User {
        def apply(name:String, age:Int): User = {
            val user = new User()
            user.name = name
            user.age = age
            user
        }

        def unapply(arg: User): Option[(String, Int)] = {
            Option( (arg.name, arg.age) )
        }
    }
    def getUser() = {
        val user = new User()
        // apply方法也可以构建对象
        user.name = "zhangsan"
        user.age = 20
        user
    }
}
